import { getCampusSelector } from '@/api/campus/campus'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import request from '@/utils/request'
import moment from 'moment'
import ContractForm from '../contract/index'
import CloseSupportForm from '../supportClose/Form/index.vue'
import ExportBox from './ExportBox'
import CustomForm from './Form'
import StatusBox from './StatusBox'
export default {
  components: { CustomForm, ExportBox, StatusBox, ContractForm, CloseSupportForm },
  name: 'campus.renewal',
  data() {
    return {
      query: {
        campusId: undefined,
        brand: 'bell',
        isPayed: undefined,
        renewalDate: [
          moment()
            .startOf('year')
            .format('YYYY-MM-DD'),
          moment()
            .endOf('year')
            .format('YYYY-MM-DD')
        ],
        area: undefined,
        type: 'franchisee'
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'asc',
        sidx: 'endDate'
      },
      formVisible: false,
      exportBoxVisible: false,
      contractFormVisible: false,
      closeSupportVisible: false,
      statusVisible: false,
      columnList: [{ prop: 'brandText', label: '校区品牌' }, { prop: 'code', label: '校区编码' }, { prop: 'fullName', label: '校区名称' }, { prop: 'typeText', label: '校区类型' }, { prop: 'levelText', label: '校区阶段' }, { prop: 'coopMode', label: '合作形式' }, { prop: 'status', label: '校区状态' }, { prop: 'provinceId', label: '省份' }, { prop: 'cityId', label: '城市' }, { prop: 'renewal', label: '品牌续约费' }, { prop: 'deposit', label: '保证金' }, { prop: 'investors', label: '投资人' }, { prop: 'investorsPhone', label: '联系电话' }, { prop: 'renewalStatus', label: '续费情况' }, { prop: 'renwelCount', label: '续费次数' }, { prop: 'startDate', label: '开始日期' }, { prop: 'endDate', label: '截止日期' }],
      campusOptions: [],
      brandOptions: [],
      statusOptions: [],
      typeOptions: [],
      isPayedOptions: [],

      alreadyRenewedCount: 0, //已续费数量
      shouldRenewCount: 0, //应续费数量
      renewalRate: 0 //转换率（已为百分比）
    }
  },
  computed: {},
  created() {
    this.initData()
    this.getBrandOptionsOptions()
    this.getCampusTypeOptions()
    this.getCampusStatusOptions()
    this.getisPayedOptions()
    this.getCampusSelector()
  },
  methods: {
    changeCloseHandle(campusId, status) {
      this.closeSupportVisible = true
      this.$nextTick(() => {
        this.$refs.CloseSupport.init(campusId, '', status)
      })
    },
    getBrandOptionsOptions() {
      getDictionaryDataSelector('Brand').then(res => {
        this.brandOptions = res.data.list
      })
    },
    getisPayedOptions() {
      getDictionaryDataSelector('RenewalStatus').then(res => {
        console.log(res.data.list)
        this.isPayedOptions = res.data.list
      })
    },
    getCampusTypeOptions() {
      getDictionaryDataSelector('CampusType').then(res => {
        this.typeOptions = res.data.list
      })
    },
    getCampusStatusOptions() {
      getDictionaryDataSelector('CampusStatus').then(res => {
        this.statusOptions = res.data.list
      })
    },
    getCampusSelector() {
      getCampusSelector({ brand: this.query.brand }).then(res => {
        this.campusOptions = res.data
      })
    },
    handleChangeExport() {
      this.btn_loading = true
      this.$message({
        message: '文件下载链接稍后推送到站内信，请在右上角铃铛图标处查收！',
        type: 'success',
        showClose: true,
        duration: 0
      })
      try {
        let _query = {
          ...this.listQuery,
          ...this.query
        }
        let query = {}
        for (let key in _query) {
          if (Array.isArray(_query[key])) {
            query[key] = _query[key].join()
          } else {
            query[key] = _query[key]
          }
        }
        request({
          url: `/api/campus/contract/Actions/Export/Renewal`,
          method: 'GET',
          data: query
        }).then(res => {
          this.btn_loading = false
        })
      } catch (e) {
        this.btn_loading = false
      }
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/campus/contract/statistics/all`,
        method: 'GET',
        data: query
      }).then(res => {
        this.alreadyRenewedCount = res.data.alreadyRenewedCount
        this.shouldRenewCount = res.data.shouldRenewCount
        this.renewalRate = res.data.renewalRate
      })
      request({
        url: `/api/campus/contract/renewal`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    ContractHandle(id) {
      this.contractFormVisible = true
      this.$nextTick(() => {
        this.$refs.ContractForm.initData(id)
      })
    },
    handleSelectionChange(val) {
      const res = val.map(item => item.id)
      this.multipleSelection = res
    },
    changeStatusHandle(campusId, status) {
      this.statusVisible = true
      this.$nextTick(() => {
        this.$refs.StatusBox.init(campusId, status)
      })
    },
    addOrUpdateHandle(row, type) {
      this.formVisible = true
      this.$nextTick(() => {
        if (type == 'update') {
          this.$refs.CustomForm.updateInit(row)
        } else {
          this.$refs.CustomForm.addInit(row)
        }
      })
    },
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    handleChangeBrand(val) {
      getCampusSelector().then(res => {
        console.log(val)
        this.campusOptions = res.data.filter(i => i.brand == val)
      })
    },
    download(data) {
      this.$message({
        message: '文件下载链接稍后推送到站内信，请在右上角铃铛图标处查收！',
        type: 'success',
        showClose: true,
        duration: 0
      })
      let _query = {
        ...this.listQuery,
        ...this.query,
        ...data
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/campus/contract/Actions/Export`,
        method: 'GET',
        data: query
      })
        .then(res => {
          this.$refs.ExportBox.visible = false
          this.exportBoxVisible = false
        })
        .catch(() => {
          this.$refs.ExportBox.loadingInstance = false
        })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'asc',
        sidx: 'endDate'
      }
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (this.closeSupportVisible) {
        this.closeSupportVisible = false
      }
      if (isrRefresh) this.reset()
    },
    reset() {
      this.query = {
        campusId: undefined,
        brand: 'bell',
        isPayed: undefined,
        renewalDate: [
          moment()
            .startOf('year')
            .format('YYYY-MM-DD'),
          moment()
            .endOf('year')
            .format('YYYY-MM-DD')
        ],
        area: undefined,
        type: 'franchisee'
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'asc',
        sidx: 'endDate'
      }
      this.initData()
    }
  }
}
